//写cookies
// function setCookie(c_name, value, expiredays){
//     var exdate=new Date();
//     exdate.setDate(exdate.getDate() + expiredays);
//     document.cookie=c_name+ "=" + escape(value) + ";path=/" + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
// }
function setCookie(name, value, seconds) {
    seconds = seconds || 0;   //seconds有值就直接赋值，没有为0，这个根php不一样。
    var expires = "";
    if (seconds != 0 ) {      //设置cookie生存时间
      var date = new Date();
      date.setTime(date.getTime()+(seconds*1000));
      expires = "; expires="+date.toGMTString();
    }
    document.cookie = name+"="+escape(value)+expires+"; path=/";   //转码并赋值
}    
//读取cookies
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

    if(arr=document.cookie.match(reg)) {
        return (arr[2]);
    } else {
        return null;
    }
}

//删除cookies
function delCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 10000);
    var cval=getCookie(name);
    if(cval!=null) {
        document.cookie= name + "="+cval+";expires="+exp.toGMTString() + "; path=/";
    }

}
// 返回
$('body').on('click', '.page-go-back', function () {
    console.log(123)
    window.history.back()
})

$('body').on('click', '.cookie_to_url', function() {
    var tourl = $(this).data('url')
    var current_url = $(this).data('current_url')
    var toac = $(this).data('toac')
    if (toac) {
        setCookie(toac, current_url, 8 * 60) // 10分钟
    }
    location.href=tourl
})

$('body').on('click', '.cookie_go_back', function() {
    var url = $(this).data('url')
    var ac = $(this).data('ac')
    if (ac) {
        console.log(ac)
        delCookie(ac)
        console.log(getCookie(ac))
        location.href=url
    } else {
        location.href=url
    }
})

function dateToHan(date)
{
    if (!date) {
        return '';
    }
    let c_date = new Date(date);
    let month = c_date.getMonth() + 1
    let day = c_date.getDate()
    return month + '月' + day + '日'
}
function dateToWeek(date)
{
    if (!date) {
        return '';
    }
    var l = ["日","一","二","三","四","五","六"];
    var d = new Date(date).getDay();
    var str = "周" + l[d];
    return str
}
function getDaysBetween(startDate, enDate) {
    if (!startDate || !enDate) {
        return ''
    }
    var sDate = Date.parse(startDate)
    var eDate = Date.parse(enDate)
    if (sDate > eDate) {
        return 0
    }
    // 这个判断可以根据需求来确定是否需要加上
    if (sDate === eDate) {
        return 1
    }
    var days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
    return days + 1
}

//处理数据
function remove_quot(obj){
    return obj.replace(/&quot;/g,"\"");
}
//替换解压json后的&lt;、&gt;
function replace_lt_gt(obj,first=8,last=128) {//32768
    let length = obj.length;
    if(obj[0] =='&lt;'+first){
        obj[0] = '<'+first;
    }
    if(obj[length-1]  == '&gt;='+last){
        obj[length-1] = '>='+last;
    }
    return obj ;
}

function loading(msg) {
    Dcat.swal.fire({
        text: msg,
        width: '20rem',
        allowOutsideClick: false,
        showCancelButton: false,
        showConfirmButton: false,
    })
}

function loadingHref(msg) {
    Dcat.swal.fire({
        text: msg,
        width: '20rem',
        timer: 500,
        allowOutsideClick: false,
        showCancelButton: false,
        showConfirmButton: false,
    })
}

var reading = false
$(".read-file").click(function() {
    var url = $(this).data('url')
    if (reading) {
        return false
    }
    reading = !reading
    loading('正在读取...')
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'html',
        success: function(res) {
            reading = !reading
            Dcat.swal.close()
            Dcat.swal.fire({
                html: res,
                grow: 'fullscreen',
                width: '100%',
                allowOutsideClick: false,
                showCancelButton: false,
                focusConfirm: false,
                confirmButtonText: '关闭',
                confirmButtonColor: '#0171fb',
                customClass: 'read-file-container'
            })
        },
        error: function(res) {
            reading = !reading
            Dcat.swal.close()
            Dcat.swal.fire({
                html: '考核表加载失败',
                grow: 'fullscreen',
                width: '100%',
                allowOutsideClick: false,
                showCancelButton: false,
                focusConfirm: false,
                confirmButtonText: '关闭',
                confirmButtonColor: '#0171fb',
            })
        }
    })
})